2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_ComponentLayoutEditor.h"
30 //==============================================================================
31 ComponentOverlayComponent::ComponentOverlayComponent (Component
* const target_
,
32 ComponentLayout
& layout_
)
38 originalAspectRatio (1.0)
40 setMinimumOnscreenAmounts (0, 0, 0, 0);
41 setSizeLimits (borderThickness
* 2 + 2, borderThickness
* 2 + 2, 8192, 8192);
43 addChildComponent (border
= new ResizableBorderComponent (this, this));
45 border
->setBorderThickness (BorderSize
<int> (borderThickness
));
47 target
->addComponentListener (this);
49 changeListenerCallback (0);
50 layout
.getSelectedSet().addChangeListener (this);
52 setRepaintsOnMouseActivity (true);
53 border
->setRepaintsOnMouseActivity (true);
56 ComponentOverlayComponent::~ComponentOverlayComponent()
58 layout
.getSelectedSet().removeChangeListener (this);
61 target
->removeComponentListener (this);
66 void ComponentOverlayComponent::changeListenerCallback (ChangeBroadcaster
*)
68 const bool nowSelected
= layout
.getSelectedSet().isSelected (target
);
70 if (selected
!= nowSelected
)
72 selected
= nowSelected
;
73 border
->setVisible (nowSelected
);
78 void ComponentOverlayComponent::paint (Graphics
& g
)
80 jassert (target
!= 0);
84 const BorderSize
<int> borderSize (border
->getBorderThickness());
86 drawResizableBorder (g
, getWidth(), getHeight(), borderSize
, (isMouseOverOrDragging() || border
->isMouseOverOrDragging()));
88 else if (isMouseOverOrDragging())
90 drawMouseOverCorners (g
, getWidth(), getHeight());
94 void ComponentOverlayComponent::resized()
96 jassert (target
!= 0);
98 border
->setBounds (0, 0, getWidth(), getHeight());
101 void ComponentOverlayComponent::mouseDown (const MouseEvent
& e
)
104 mouseDownSelectStatus
= layout
.getSelectedSet().addToSelectionOnMouseDown (target
, e
.mods
);
106 if (e
.mods
.isPopupMenu())
109 return; // this may be deleted now..
113 void ComponentOverlayComponent::mouseDrag (const MouseEvent
& e
)
115 if (! e
.mods
.isPopupMenu())
117 if (selected
&& ! dragging
)
119 dragging
= ! e
.mouseWasClicked();
122 layout
.startDragging();
127 layout
.dragSelectedComps (e
.getDistanceFromDragStartX(),
128 e
.getDistanceFromDragStartY());
133 void ComponentOverlayComponent::mouseUp (const MouseEvent
& e
)
136 layout
.endDragging();
138 layout
.getSelectedSet().addToSelectionOnMouseUp (target
, e
.mods
, dragging
, mouseDownSelectStatus
);
141 void ComponentOverlayComponent::componentMovedOrResized (Component
& component
, bool wasMoved
, bool wasResized
)
143 updateBoundsToMatchTarget();
146 void ComponentOverlayComponent::updateBoundsToMatchTarget()
148 Component
* const parent
= target
->getParentComponent();
149 jassert (parent
!= 0);
153 const int dx
= parent
->getX();
154 const int dy
= parent
->getY();
156 setBounds (dx
+ target
->getX() - borderThickness
,
157 dy
+ target
->getY() - borderThickness
,
158 target
->getWidth() + borderThickness
* 2,
159 target
->getHeight() + borderThickness
* 2);
162 if (border
->isMouseButtonDown())
166 void ComponentOverlayComponent::resizeStart()
169 originalAspectRatio
= getWidth() / (double) getHeight();
171 originalAspectRatio
= 1.0;
173 layout
.getDocument()->getUndoManager().beginNewTransaction ("Resize components");
176 void ComponentOverlayComponent::resizeEnd()
178 layout
.getDocument()->getUndoManager().beginNewTransaction();
181 void ComponentOverlayComponent::checkBounds (Rectangle
<int>& bounds
,
182 const Rectangle
<int>& previousBounds
,
183 const Rectangle
<int>& limits
,
184 const bool isStretchingTop
,
185 const bool isStretchingLeft
,
186 const bool isStretchingBottom
,
187 const bool isStretchingRight
)
189 if (ModifierKeys::getCurrentModifiers().isShiftDown())
190 setFixedAspectRatio (originalAspectRatio
);
192 setFixedAspectRatio (0.0);
194 ComponentBoundsConstrainer::checkBounds (bounds
, previousBounds
, limits
, isStretchingTop
, isStretchingLeft
, isStretchingBottom
, isStretchingRight
);
196 if (layout
.getDocument()->isSnapActive (true))
198 Component
* const parent
= target
->getParentComponent();
199 jassert (parent
!= 0);
203 const int dx
= parent
->getX();
204 const int dy
= parent
->getY();
206 int x
= bounds
.getX();
207 int y
= bounds
.getY();
208 int w
= bounds
.getWidth();
209 int h
= bounds
.getHeight();
211 x
+= borderThickness
- dx
;
212 y
+= borderThickness
- dy
;
213 w
-= borderThickness
* 2;
214 h
-= borderThickness
* 2;
219 if (isStretchingRight
)
220 right
= layout
.getDocument()->snapPosition (right
);
222 if (isStretchingBottom
)
223 bottom
= layout
.getDocument()->snapPosition (bottom
);
225 if (isStretchingLeft
)
226 x
= layout
.getDocument()->snapPosition (x
);
229 y
= layout
.getDocument()->snapPosition (y
);
231 w
= (right
- x
) + borderThickness
* 2;
232 h
= (bottom
- y
) + borderThickness
* 2;
233 x
-= borderThickness
- dx
;
234 y
-= borderThickness
- dy
;
236 bounds
= Rectangle
<int> (x
, y
, w
, h
);
241 void ComponentOverlayComponent::applyBoundsToComponent (Component
* component
, const Rectangle
<int>& bounds
)
243 if (component
->getBounds() != bounds
)
245 layout
.getDocument()->getUndoManager().undoCurrentTransactionOnly();
247 component
->setBounds (bounds
);
249 Component
* const parent
= target
->getParentComponent();
250 jassert (parent
!= 0);
254 target
->setBounds (bounds
.getX() + borderThickness
- parent
->getX(),
255 bounds
.getY() + borderThickness
- parent
->getY(),
256 bounds
.getWidth() - borderThickness
* 2,
257 bounds
.getHeight() - borderThickness
* 2);
260 layout
.updateStoredComponentPosition (target
, true);
264 void ComponentOverlayComponent::showPopupMenu()
266 ComponentTypeHandler::getHandlerFor (*target
)->showPopupMenu (target
, layout
);